<?php
/**
 * Here is your custom functions.
 */
if (!function_exists('ok_rsp')) {
    function ok_rsp($msg = '', $data = [], $code = 0) {
        return json_encode([
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ]);
    }
}

if (!function_exists('err_rsp')) {
    function err_rsp ($msg, $code = 400, $data = []) {
        return json_encode([
            'code' => $code,
            'msg' => $msg,
            'data' => $data
        ]);
    }
}

function admin_public_path(string $path = ''): string
{
    static $publicPath = '';
    if (!$publicPath) {
        $publicPath = \config('app.admin_public_path');
    }

    $path = str_replace('/app/admin', '', $path);

    return path_combine($publicPath, $path);
}

if (!function_exists('e_rsp')) {
    function e_rsp(Exception $e, $data = []) {
        if (getenv('APP_DEBUG')) {
            echo $e->getMessage() . $e->getTraceAsString();
        }

        return json_encode([
            'code' => $e->getCode() > 0 ? $e->getCode() : 400,
            'msg' => $e->getMessage(),
            'data' => $data
        ]);
    }
}

if (!function_exists('get_user_id')) {
    function get_user_id() {
        $request = request();
        $token = $request->header('token');
        return \app\services\TokenService::getUserId($token);
    }
}

if (!function_exists('format_mobile')) {
    function format_mobile($mobile) {
        if ($mobile) {
            return substr($mobile, 0, 3) . '****' . substr($mobile, 7);
        }

        return $mobile;
    }
}

if (!function_exists('format_bank_card')) {
    function format_bank_card($mobile) {
        if ($mobile) {
            return substr($mobile, 0, 4) . '********' . substr($mobile, 12);
        }

        return $mobile;
    }
}

if (!function_exists('raw_json_rsp')) {
    function raw_json_rsp($data) {
        return json_encode($data);
    }
}

if (!function_exists('full_url')) {
    function full_url($url) {
        if (strpos($url, 'http') !== false) {
            return $url;
        }
        return getenv('APP_URL') . $url;
    }
}

if (!function_exists('cartesian_product')) {
    function cartesian_product($arrays) {
        $result = [[]];

        foreach ($arrays as $array) {
            $tmp = [];

            foreach ($result as $resultItem) {
                foreach ($array as $item) {
                    $tmp[] = array_merge($resultItem, [$item]);
                }
            }

            $result = $tmp;
        }

        return $result;
    }
}

if (!function_exists('diff_time')) {
    function diff_time($datetime) {
        $diff = (new DateTime())->diff(new DateTime($datetime));
        $year = $diff->y;
        if ($year > 0) {
            return $year . '年前';
        }
        $month = $diff->m;

        if ($month > 0) {
            return $month . '月前';
        }

        $day = $diff->days;

        if ($day > 0) {
            return $day . '天前';
        }
        $hour = $diff->h;

        if ($hour > 0) {
            return $hour . '小时前';
        }

        $minute = $diff->i;

        if ($minute > 0) {
            return $minute . '分钟前';
        }

        $seconds = $diff->s;

        if ($seconds > 0) {
            return $seconds . '秒前';
        }

        return '刚刚';
    }
}

function myUrlEncode($string) {
    $entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
    $replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");
    return str_replace($entities, $replacements, urlencode($string));
}

function getWeek($timestamp = null, $prefix = '周')
{
    if (is_null($timestamp)) {
        $timestamp = time();
    }
    $weekArray=array("日","一","二","三","四","五","六"); //先定义一个数组
    return $prefix . $weekArray[date("w", $timestamp)]; //w为星期几的数字形式,这里0为周日
}

function hideTrueName($name)
{
    $hiddenName = str_replace($name[1], "*", $name); // 将第一个字母替换成*号
    return $hiddenName;
}

function hideIdCardNo($no)
{
    return substr($no, 0, 6) . '********' . substr($no, 14);
}

//根据两点经纬度计算距离
//纬度1、纬度2、经度1、经度2
function get_two_point_distance($lat1,$lat2,$lng1,$lng2)
{

    $radLat1 = deg2rad($lat1);//deg2rad()函数将角度转换为弧度
    $radLat2 = deg2rad($lat2);
    $radLng1 = deg2rad($lng1);
    $radLng2 = deg2rad($lng2);
    $a = $radLat1 - $radLat2;
    $b = $radLng1 - $radLng2;
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137;
    return round($s,2);//返回公里数
}